<?php
Junction_FileCabinet::using("Junction_Expression_Parser");
Junction_FileCabinet::using("Junction_Expression_RecursiveDescent");
Junction_FileCabinet::using("Junction_Expression_Exception");

class Test_Junction_Expression_Parser extends UnitTestCase {
	
	private $_parser;
	
	private $_trivialExpression = '1';
	
	private $_binaryExpressionAnd = 'Foo && Bar';
	private $_binaryExpressionOr = 'Foo || Bar';
	private $_binaryExpressionGt = 'Foo > Bar';
	private $_binaryExpressionLt = 'Foo < Bar';
	private $_binaryExpressionGte = 'Foo >= Bar';
	private $_binaryExpressionLte = 'Foo <= Bar';
	private $_binaryError1 = 'FOO &&';
	private $_binaryError2 = 'FOO BAR';
	
	private $_binaryComplexExpression = '(Foo && Bar) || (Bar || Foo)';
	
	private $_unaryExpressionNot = '!Foo';
	private $_unaryError = '!';
		
	private $_groupingExpression = '(Foo)';
	private $_groupingError1 = '(FOO ';
	private $_groupingError2 = 'FOO)';
	 
	public function __construct()
	{
		$this->_parser = new Junction_Expression_Parser();
	}

	public function testTrivialExpressions() 
	{	
		$this->assertTrue($this->_parser->parse($this->_trivialExpression));
	}
	
	public function testBinaryOperators()
	{
		$this->assertTrue($this->_parser->parse($this->_binaryExpressionAnd));
		$this->assertTrue($this->_parser->parse($this->_binaryExpressionOr));
		$this->assertTrue($this->_parser->parse($this->_binaryExpressionGt));
		$this->assertTrue($this->_parser->parse($this->_binaryExpressionLt));
		// $this->assertTrue($this->_parser->parse($this->_binaryExpressionGte));
		// $this->assertTrue($this->_parser->parse($this->_binaryExpressionLte));
		
		try {
			$this->assertTrue($this->_parser->parse($this->_binaryError1));
			$this->fail();
		} catch (Junction_Expression_Exception $e) {
			$this->pass();
		}		
		
		try {
			$this->assertTrue($this->_parser->parse($this->_binaryError2));
			$this->fail();
		} catch (Junction_Expression_Exception $e) {
			$this->pass();
		}	
		
		$this->assertTrue($this->_parser->parse($this->_binaryComplexExpression));
	}
	
	public function testUnaryOperators()
	{
		$this->assertTrue($this->_parser->parse($this->_binaryComplexExpression));
		
		try {
			$this->assertTrue($this->_parser->parse($this->_unaryError));
			$this->fail();
		} catch (Junction_Expression_Exception $e) {
			$this->pass();
		}			
	}
	
	public function testGrouping()
	{
		$this->assertTrue($this->_parser->parse($this->_groupingExpression));
		
		try {
			$this->assertTrue($this->_parser->parse($this->_groupingError1));
			$this->fail();
		} catch (Junction_Expression_Exception $e) {
			$this->pass();
		}	
		
		try {
			$this->assertTrue($this->_parser->parse($this->_groupingError2));
			$this->fail();
		} catch (Junction_Expression_Exception $e) {
			$this->pass();
		}					
	}
}
?>